package com.taotao.service;

import com.taotao.config.RateLimiter;
import com.taotao.ext.LeakyBuket;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author wangjin
 * @title: RateLimiterService
 * @projectName sw-gateway
 * @description:
 * @date 2021/12/19 19:51
 */
@RestController
@Slf4j
public class RateLimiterService {
    RateLimiter rateLimiter = RateLimiter.create(2);

    /**
     * 手写限流
     *
     * @return
     */
    @RequestMapping("/ratelimiter")
    public String ratelimiter() {
        boolean result = rateLimiter.tryAcquire();
        if (result) {
            return "success";
        } else {
            return "限流了";
        }

    }

    /**
     * 演示漏桶算法 纯手写注解
     *
     * @return
     */
    @RequestMapping("/getMayiktLeakyBucket")
    @LeakyBuket(name = "getMayiktLeakyBucket", threads = 2)
    public String getMayiktLeakyBucket() {
        log.info("正常执行业务逻辑代码...");
        try {
            Thread.sleep(3000);
        } catch (Exception e) {

        }
        return "mayikt";
    }
}
